/**
 * @file       UserRequest.cc
 * @brief      
 * @author     LiuJkun
 * @version    1.0.0
 */

/*********************************************************************
 *  LiuJikun   10/25/09      Initial Version
 *********************************************************************/
#include	<stdio.h>
#include	<string.h>
#include	<arpa/inet.h>

#include	"UserRequest.h"

/**
 * @brief  constructor for class UserRequest
 */
//--------------------------------------------------------------------------------------
UserRequest::UserRequest ()
{
}

/**
 * @brief 
 * @return
 */
void UserRequest::_compose_request ( void )
{
    _m_request.clear();
    int32_t totalLen = 16 + ELEMLEN_DATE + ELEMLEN_TITLE + ELEMLEN_PATH + ELEMLEN_URL + _m_text.size();
    char buf[ MAX_ELEMENT_LEN ];

    memset( buf, 0, MAX_ELEMENT_LEN );
    *(int32_t*)buf = htonl( totalLen );
    _m_request.append( buf, 4 );

    memset( buf, 0, MAX_ELEMENT_LEN );
    *(int32_t*)buf = htonl( 0x00002000 );
    _m_request.append( buf, 4 );

    memset( buf, 0, MAX_ELEMENT_LEN );
    *(int32_t*)buf = htonl( 0x00000011 );
    _m_request.append( buf, 4 );

    memset( buf, 0, MAX_ELEMENT_LEN );
    *(int32_t*)buf = htonl( _m_id );
    _m_request.append( buf, 4 );

    memset( buf, 0, MAX_ELEMENT_LEN );
    memcpy( buf, _m_date.c_str(), _m_date.size() > ELEMLEN_DATE ? ELEMLEN_DATE : _m_date.size() );
    _m_request.append( buf, ELEMLEN_DATE );

    memset( buf, 0, MAX_ELEMENT_LEN );
    memcpy( buf, _m_title.c_str(), _m_title.size() > ELEMLEN_TITLE? ELEMLEN_TITLE : _m_title.size() );
    _m_request.append( buf, ELEMLEN_TITLE );

    memset( buf, 0, MAX_ELEMENT_LEN );
    memcpy( buf, _m_path.c_str(), _m_path.size() > ELEMLEN_PATH? ELEMLEN_PATH : _m_path.size() );
    _m_request.append( buf, ELEMLEN_PATH );

    memset( buf, 0, MAX_ELEMENT_LEN );
    memcpy( buf, _m_url.c_str(), _m_url.size() > ELEMLEN_URL? ELEMLEN_URL : _m_url.size() );
    _m_request.append( buf, ELEMLEN_URL );
    _m_request.append( _m_text.c_str(), _m_text.size() );
    printf ( "totalLen = %d, size = %d\n", totalLen, _m_request.size() );
}		

/**
 * @brief  constructor for class UserResult
 */
//--------------------------------------------------------------------------------------
UserResult::UserResult ()
{
}

/**
 * @brief 
 * @return
 */
void UserResult::parse_result ( std::string &result )
{
    const char *ptr = result.c_str();
    if ( result.size() < 12  ) {
        printf ( "invalid result.\n" );
        return;
    }
    _m_length = ntohl( *(int32_t*) (ptr) );
    _m_type = ntohl( *(int32_t*) (ptr + 4) );
    _m_subType = ntohl( *(int32_t*) (ptr + 8) );
    printf ( "\tlength = %d\n\ttype = %d\n\tsubType = %d\n", _m_length, _m_type, _m_subType  );

    return ;
}		

